<template>
  <div class="auth-container">
    <div class="auth-form">
      <h3 class="auth-title">用户注册</h3>
      <el-form ref="registerFormRef" :model="registerState.registerForm" :rules="registerState.registerRules" status-icon label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input 
            v-model="registerState.registerForm.username"
            placeholder="请输入用户名" 
            autocomplete="off"
          />
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input 
            v-model="registerState.registerForm.password" 
            type="password" 
            placeholder="请输入密码" 
            autocomplete="off"
          />
        </el-form-item>
        
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input 
            v-model="registerState.registerForm.confirmPassword" 
            type="password" 
            placeholder="请确认密码" 
            autocomplete="off"
          />
        </el-form-item>
        
        <el-form-item label="真实姓名" prop="realName">
          <el-input 
            v-model="registerState.registerForm.realName" 
            placeholder="请输入真实姓名" 
            autocomplete="off"
          />
        </el-form-item>
        
        <el-form-item label="邮箱" prop="email">
          <el-input 
            v-model="registerState.registerForm.email" 
            placeholder="请输入邮箱" 
            autocomplete="off"
          />
        </el-form-item>
        
        <el-form-item label="手机号" prop="phone">
          <el-input 
            v-model="registerState.registerForm.phone" 
            placeholder="请输入手机号" 
            autocomplete="off"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button 
            type="primary" 
            class="auth-form-button" 
            :loading="loading" 
            @click="handleRegister"
          >
            注册
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="auth-form-footer">
        <span>已有账号?</span>
        <router-link to="/login">立即登录</router-link>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, reactive, computed } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

export default {
  name: 'Register',
  setup() {
    const store = useStore()
    const router = useRouter()
    const registerFormRef = ref(null)
    
    const registerState = reactive({
      registerForm: {
        username: '',
        password: '',
        confirmPassword: '',
        realName: '',
        email: '',
        phone: ''
      },
      registerRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 4, max: 20, message: '用户名长度必须在4-20位之间', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '密码长度必须在6-20位之间', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { 
            validator: (rule, value, callback) => {
              if (value === '') {
                callback(new Error('请再次输入密码'))
              } else if (value !== registerState.registerForm.password) {
                callback(new Error('两次输入密码不一致'))
              } else {
                callback()
              }
            }, 
            trigger: 'blur' 
          }
        ],
        realName: [
          { required: true, message: '请输入真实姓名', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
        ],
        phone: [
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号格式', trigger: 'blur' }
        ]
      }
    })
    
    const loading = computed(() => store.state.loading)
    
    const handleRegister = () => {
      if (!registerFormRef.value) return
      
      registerFormRef.value.validate(async (valid) => {
        if (valid) {
          // 构建注册请求对象
          const registerData = {
            username: registerState.registerForm.username,
            password: registerState.registerForm.password,
            realName: registerState.registerForm.realName,
            email: registerState.registerForm.email,
            phone: registerState.registerForm.phone
          }
          
          try {
            const response = await store.dispatch('register', registerData)
            ElMessage.success('注册成功，请登录')
            router.push('/login')
          } catch (error) {
            // 根据后端错误消息显示不同的提示
            let errorMsg = '注册失败，请重试'
            if (error.response && error.response.data && error.response.data.message) {
              errorMsg = error.response.data.message
            }
            ElMessage.error(errorMsg)
          }
        }
      })
    }
    
    return {
      registerFormRef,
      registerState,
      loading,
      handleRegister
    }
  }
}
</script>

<style scoped>
.auth-form {
  min-width: 450px;
}
</style> 
 